<script setup lang="ts">
import type { ConsultIllness } from "@/types/consult";
import { computed, onMounted, reactive, ref, watch } from "vue";
import type { UploaderAfterRead } from "vant/lib/uploader/types";
import { IllnessTime } from "@/enums";
import { uploadImage } from "@/services/consult";
import { useConsultStore } from "@/stores";
import { useRouter } from "vue-router";
import { showConfirmDialog } from "vant";
const router = useRouter();
const store = useConsultStore();
const timeOptions = [
  { label: "一周内", value: IllnessTime.Week },
  { label: "一月内", value: IllnessTime.Month },
  { label: "半年内", value: IllnessTime.HalfYear },
  { label: "大于半年", value: IllnessTime.More },
];
const flagOptions = [
  { label: "就诊过", value: 1 },
  { label: "没就诊过", value: 0 },
];
const form = ref<ConsultIllness>({
  illnessDesc: "",
  illnessTime: undefined,
  consultFlag: undefined,
  pictures: [],
});
const fileList = ref<any[]>([]);
const afterRead: UploaderAfterRead = async (file) => {
  if (Array.isArray(file)) return;
  if (!file.file) return;
  file.status = "uploading";
  file.message = "上传中...";
  // 此时可以自行将文件上传至服务器
  uploadImage(file.file)
    .then((res) => {
      file.status = "done";
      file.message = "上传完成";
      file.url = res.data.url;
      form.value.pictures?.push(res.data);
    })
    .catch(() => {
      file.status = "failed";
      file.message = "上传失败";
    });
};
// 计算输入框是否为空 
const disabled = computed(
  () =>
    !form.value.consultFlag === undefined ||
    form.value.illnessTime === undefined ||
    form.value.illnessDesc === "" ||
    form.value.pictures?.length === 0
);

// 点击下一步
const next = () => {
  if (disabled.value) return;
  store.setIllness(form.value);
  router.push("/user/patient?isChange=1");
};
// 回填
onMounted(() => {
  if (store.consult.illnessDesc) {
    showConfirmDialog({
      title: "温馨提示",
      message: "是否恢复之前填写的病情信息？",
      closeOnPopstate: false,
    }).then(() => {
      // 回显数据
      const { illnessDesc, illnessTime, consultFlag, pictures } = store.consult;
      form.value = { illnessDesc, illnessTime, consultFlag, pictures };
      fileList.value = pictures || [];
    });
  }
});
</script>

<template>
  <div class="consult-illness-page">
    <cp-nav-bar title="图文问诊" />
    <!-- 医生提示 -->
    <div class="illness-tip van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <div class="info">
        <p class="tit">在线医生</p>
        <p class="tip">
          请描述你的疾病或症状、是否用药、就诊经历，需要我听过什么样的帮助
        </p>
        <p class="safe">
          <cp-icon name="consult-safe" /><span>内容仅医生可见</span>
        </p>
      </div>
    </div>
    <!-- 收集信息 -->
    <div class="illness-form">
      <van-field
        type="textarea"
        rows="3"
        placeholder="请详细描述您的病情，病情描述不能为空"
        v-model="form.illnessDesc"
      ></van-field>
      <div class="item">
        <p>本次患病多久了？</p>
        <cp-radio-btn :options="timeOptions" v-model="form.illnessTime" />
      </div>
      <div class="item">
        <p>此次病情是否去医院就诊过？</p>
        <cp-radio-btn :options="flagOptions" v-model="form.consultFlag" />
      </div>
      <div class="uoloadImg">
        <van-uploader
          style="display: flex; flex-wrap: wrap"
          upload-icon="photo-o"
          upload-text="上传图片"
          :max-size="500 * 1024"
          :max-count="9"
          :after-read="afterRead"
          v-model="fileList"
        />
        <p class="tip" v-if="fileList.length == 0">
          上传内容仅医生可见，最多上传9张图片，最大5MB
        </p>
      </div>
      <van-button type="primary" block round @click="next" :disabled="disabled">
        下一步
      </van-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.illness-tip {
  display: flex;
  padding: 15px;
  .img {
    width: 52px;
    height: 52px;
    border-radius: 4px;
    overflow: hidden;
    margin-top: 10px;
  }
  .info {
    flex: 1;
    padding-left: 12px;
    .tit {
      font-size: 16px;
      margin-bottom: 5px;
    }
    .tip {
      padding: 12px;
      background: var(--cp-bg);
      color: var(--cp-text3);
      font-size: 13px;
      margin-bottom: 10px;
    }
    .safe {
      font-size: 10px;
      color: var(--cp-text3);
      display: flex;
      align-items: center;
      .cp-icon {
        font-size: 12px;
        margin-right: 2px;
      }
    }
  }
}
.illness-form {
  padding: 10px;
  .van-field {
    padding: 0;
    &::after {
      border-bottom: none;
    }
  }
  .item {
    > p {
      color: var(--cp-text3);
      padding: 15px 0;
    }
  }
}
.uoloadImg {
  display: flex;
  align-items: center;
  .tip {
    font-size: 10px;
    color: var(--cp-text3);
  }
  .uploader {
    width: 80px;
    height: 80px;
    border-radius: 10px;
    text-align: center;
    background-color: #f7f8fa;
    .van-icon {
      margin-top: 20px;
    }
  }
}
</style>
